//
//  HYMeetingListCell.m
//  Booking
//
//  Created by Duntech on 2018/4/20.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYMeetingListCell.h"
#import "HYMeetingRoomModel.h"

@interface HYMeetingListCell ()

@property (nonatomic,strong) UIImageView *iconImgView;
@property (nonatomic,strong) UILabel *availiableLabel;
@property (nonatomic,strong) UILabel *roomNameLabel;
@property (nonatomic,strong) UILabel *deviceLabel;
@property (nonatomic,strong) UILabel *floorLabel;
@property (nonatomic,strong) UILabel *capacityLabel;
@property (nonatomic,strong) UIView *line;
@property (nonatomic,strong) NSMutableArray *deviceImgViewArray;

@end


@implementation HYMeetingListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupSubviews];
        [self setupMasonryLayout];
        [self createDeviceImgView];
    }
    return self;
}

- (void)setupSubviews{
    
    [self.contentView addSubview:self.iconImgView];
    [self.contentView addSubview:self.availiableLabel];
    [self.contentView addSubview:self.roomNameLabel];
    [self.contentView addSubview:self.deviceLabel];
    [self.contentView addSubview:self.floorLabel];
    [self.contentView addSubview:self.capacityLabel];
    [self.contentView addSubview:self.line];
}

- (void)createDeviceImgView{
    
    self.deviceImgViewArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 5; i++) {
        
        UIImageView *deviceImgView = [UIImageView new];
        deviceImgView.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:deviceImgView];
        deviceImgView.hidden = YES;
        [self.deviceImgViewArray addObject:deviceImgView];
        
        [deviceImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.roomNameLabel).offset(i * 30 * WIDTH_MULTIPLE);
            make.top.equalTo(self.deviceLabel);
            make.size.mas_equalTo(CGSizeMake(20 * WIDTH_MULTIPLE, 20 * WIDTH_MULTIPLE));
        }];
    }
}

- (void)setupMasonryLayout{
    
    [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.equalTo(self).offset(12 * WIDTH_MULTIPLE);
        make.bottom.equalTo(self).offset(-12 * WIDTH_MULTIPLE);
        make.width.mas_equalTo(self.iconImgView.mas_height).multipliedBy(1.1);
    }];
    
//    CGFloat labelWidth = [@"Available" widthForFont:KFitFont(14)] + 5;
    [_availiableLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.iconImgView.mas_right).offset(10 * WIDTH_MULTIPLE);
        make.top.equalTo(self.iconImgView).offset(2);
        make.height.mas_equalTo(20 * WIDTH_MULTIPLE);
    }];
    
    [_roomNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.availiableLabel.mas_bottom).offset(16 * WIDTH_MULTIPLE);
        make.left.equalTo(self.availiableLabel);
        make.size.mas_equalTo(CGSizeMake(180 * WIDTH_MULTIPLE, 20 * WIDTH_MULTIPLE));
    }];
    
    [_deviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.availiableLabel);
        make.top.equalTo(self.roomNameLabel.mas_bottom).offset(13 * WIDTH_MULTIPLE);
        make.right.equalTo(self).offset(-10);
    }];
    
    [_capacityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.contentView).offset(-10 * WIDTH_MULTIPLE);
        make.bottom.equalTo(self.iconImgView);
        make.size.mas_equalTo(CGSizeMake(90 * WIDTH_MULTIPLE, 20 * WIDTH_MULTIPLE));
    }];
    
    [_floorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self.capacityLabel);
        make.left.equalTo(self.roomNameLabel);
        make.right.equalTo(self.capacityLabel.mas_left).offset(-20 * WIDTH_MULTIPLE);
    }];

    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)configWithModel:(id)model{
    
    HYMeetingRoomModel *roomModel = (HYMeetingRoomModel *)model;
    
    [_iconImgView sd_setImageWithURL:[NSURL URLWithString:roomModel.imgUrl] placeholderImage:[UIImage imageNamed:@"image_placeholder"]];
    _roomNameLabel.text = roomModel.name;
    _floorLabel.text = [NSString stringWithFormat:@"%@",roomModel.floorName];
    _capacityLabel.text = [NSString stringWithFormat:@"%@:%@",NSLocalizedString(@"capacity", nil),roomModel.capacity];
    _availiableLabel.text = [roomModel.status integerValue] == 1 ? NSLocalizedString(@"available", nil) : NSLocalizedString(@"unavailable", nil);
    _availiableLabel.backgroundColor = [roomModel.status integerValue] == 1 ?  KAPP_Available_COLOR : [UIColor lightGrayColor];
    
    _deviceLabel.hidden = YES;
    //设备
    NSArray *strArray = [roomModel.type componentsSeparatedByString:@","];
    for (NSInteger i = 0; i < strArray.count; i++) {

        UIImageView *deviceImgView = self.deviceImgViewArray[i];
        deviceImgView.hidden = NO;
        //投影仪
        if ([strArray[i] isEqualToString:@"1"]) {
            deviceImgView.image = [UIImage imageNamed:@"room-projector-s"];
        }
        //电视
        if ([strArray[i] isEqualToString:@"2"]) {
            deviceImgView.image = [UIImage imageNamed:@"room-tv-s"];
        }
        //语音设备
        if ([strArray[i] isEqualToString:@"3"]) {
            deviceImgView.image = [UIImage imageNamed:@"room-sound-s"];
        }
        //多媒体
        if ([strArray[i] isEqualToString:@"4"]) {
            deviceImgView.image = [UIImage imageNamed:@"room-polycom-s"];
        }
        //视频会议
        if ([strArray[i] isEqualToString:@"5"]) {
            deviceImgView.image = [UIImage imageNamed:@"room-vc-s"];
        }
    }
}

#pragma mark - lazyload
- (UIImageView *)iconImgView{
    
    if (!_iconImgView) {
        
        _iconImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image_placeholder"]];
        _iconImgView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImgView.clipsToBounds = YES;
        _iconImgView.layer.cornerRadius = 4 * WIDTH_MULTIPLE;
    }
    return _iconImgView;
}

- (UILabel *)availiableLabel{
    
    if (!_availiableLabel) {
        _availiableLabel = [UILabel new];
        _availiableLabel.textAlignment = NSTextAlignmentCenter;
        _availiableLabel.font = KFitFont(13);
        _availiableLabel.textColor = KAPP_WHITE_COLOR;
        _availiableLabel.backgroundColor = KAPP_Available_COLOR;
        _availiableLabel.text = NSLocalizedString(@"available", nil);
        _availiableLabel.layer.cornerRadius = 2 * WIDTH_MULTIPLE;
        _availiableLabel.clipsToBounds = YES;
    }
    return _availiableLabel;
}

- (UILabel *)roomNameLabel{
    
    if (!_roomNameLabel) {
        _roomNameLabel = [UILabel new];
        _roomNameLabel.textAlignment = NSTextAlignmentLeft;
        _roomNameLabel.font = KFitBoldFont(15);
        _roomNameLabel.textColor = KAPP_BLACK_COLOR;
    }
    return _roomNameLabel;
}

- (UILabel *)deviceLabel{
    
    if (!_deviceLabel) {
        _deviceLabel = [UILabel new];
        _deviceLabel.textAlignment = NSTextAlignmentLeft;
        _deviceLabel.font = KFitFont(15);
        _deviceLabel.textColor = KAPP_BLACK_COLOR;
    }
    return _deviceLabel;
}

- (UILabel *)floorLabel{
    
    if (!_floorLabel) {
        _floorLabel = [UILabel new];
        _floorLabel.textAlignment = NSTextAlignmentRight;
        _floorLabel.font = KFitFont(12);
        _floorLabel.textColor = KCOLOR(@"848484");
    }
    return _floorLabel;
}

- (UILabel *)capacityLabel{
    
    if (!_capacityLabel) {
        _capacityLabel = [UILabel new];
        _capacityLabel.textAlignment = NSTextAlignmentLeft;
        _capacityLabel.font = KFitFont(12);
        _capacityLabel.textColor = KCOLOR(@"848484");
        _capacityLabel.text = NSLocalizedString(@"capacity", nil);
    }
    return _capacityLabel;
}

- (UIView *)line{
    
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = KAPP_SEPERATOR_COLOR;
    }
    return _line;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
